package net.wanho.index.controller;

import com.alibaba.fastjson.JSONObject;
import net.wanho.commons.factory.BeanFactory;
import net.wanho.commons.util.MD5Util;
import net.wanho.customerManage.service.BusinessService;
import net.wanho.systemSettings.service.EmployeeService;
import net.wanho.systemSettings.service.MenuService;
import net.wanho.systemSettings.vo.EmployeeVo;
import net.wanho.systemSettings.vo.MenuVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet{
	private EmployeeService employeeService = BeanFactory.getBean(EmployeeService.class) ;
	private MenuService menuService = BeanFactory.getBean(MenuService.class) ;

	
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getParameter("method");
		try {
			Method method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.setAccessible(true);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	protected void loginCheck(HttpServletRequest request,HttpServletResponse response){
		String employeeId = request.getParameter("employeeId");
		String password = request.getParameter("password");
		JSONObject json = new JSONObject();

		try {
			EmployeeVo employeeVo = employeeService.selectByIdEmployeeVo(Integer.parseInt(employeeId));
			if (employeeVo==null || !MD5Util.md5(password).equals(employeeVo.getEmmPassword())){
				json.put("status", "0");
				json.put("msg", "工号或密码错误");
				response.getWriter().write(json.toString());
				return;
			}
			if ("1".equals(employeeVo.getStatus())){
				json.put("status", "0");
				json.put("msg", "账号锁定");
				response.getWriter().write(json.toString());
				return;
			}
			request.getSession().setAttribute("employeeVo",employeeVo);
			json.put("status", "1");
			response.getWriter().write(json.toString());
		} catch (Exception e) {

			e.printStackTrace();
		}
		
	}
	
	protected void loginToCms(HttpServletRequest request,HttpServletResponse response){
		try {
		    //从session中 获得当前登录的用户
			EmployeeVo employeeVo = (EmployeeVo) request.getSession().getAttribute("employeeVo");
			//获得职位id
			Integer positonId = employeeVo.getPositonId();
			//调用menuservice的方法  所有的menu
			List<MenuVo> menuVoList = menuService.selectMenuVoByPositionId(positonId);
			employeeVo.setMenuVoList(menuVoList);
			//设置到
			response.sendRedirect(request.getContextPath()+ "/ControlServlet?method=countAll");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void updatePassword(HttpServletRequest request,HttpServletResponse response){
		JSONObject json = new JSONObject();
			json.put("status", "1");
			json.put("msg", "修改成功");
		try {
			response.getWriter().write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	protected void logout(HttpServletRequest request,HttpServletResponse response){
		try {
			response.sendRedirect(request.getContextPath()+ "/login.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
